#pragma once

#include<iostream>
#include<stdbool.h>
#include<assert.h>
using namespace std;
namespace TK
{
	template<class T>

	struct Listnode
	{
		T _data;
		Listnode<T>* _next;
		Listnode<T>* _prev;
		Listnode(const T& x = T())
			:_data(x)
			, _next(nullptr)
			, _prev(nullptr)
		{

		}
	};

	template<class T, class Raf, class Ptr>
	struct list_iterator
	{
		typedef Listnode<T> Node;
		typedef list_iterator<T, Raf, Ptr> Self;
		Node* _node;
		list_iterator(Node* node)
			:_node(node)
		{

		}
		Raf operator*()
		{
			return _node->_data;
		}
		Ptr operator->()
		{
			return &_node->_data;
		}
		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}

		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}

		bool operator != (const Self& s)
		{
			return _node != s._node;
		}
	};
	template<class T>

	class list
	{
		typedef Listnode<T> Node;
	public:
		typedef list_iterator<T, T&, T*>iterator;
		typedef list_iterator<T, const T&, const T*>const_iterator;
		//typedef list_const_iterator<T>const_iterator;

		iterator begin()
		{
			return iterator(_head->_next);
		}
		iterator begin()const
		{
			return iterator(_head->_next);
		}
		iterator end()
		{
			return iterator(_head);
		}
		iterator end()const
		{
			return iterator(_head);
		}
		void empty_init()
		{
			_head = new Node();
			_head->_next = _head;
			_head->_prev = _head;
		}

		list(size_t n = 0, const T& val = T())
		{
			empty_init();
			for (size_t i = 0; i < n; i++)
			{
				push_back(val);
			}
		}

		list(const list<T>& val)
		{
			empty_init();
			for (auto& e : val)
			{
				push_back(e);
			}
		}

		list<T>& operator=(const list<T> lt)
		{
			swap(lt);
			return *this;
		}
		void swap(const list<T>& lt)
		{
			std::swap(_head, lt.swap);
		}
		~list()
		{
			clear();
			delete _head;
			_head = nullptr;

		}
		void clear()
		{
			auto it = begin();
			while (it != end())
			{
				it=erase (it);
			}
		}
		void push_back(int x)
		{
			//Node* new_node = new Node(x);
			//Node* tail = _head->_prev;
			//tail->_next = new_node;
			//new_node->_prev = tail;
			//new_node->_next = _head;
			//_head->_prev = new_node;
			insert(end(), x);
		}
		void push_front(const T& x)
		{
			insert(begin(), x);
		}
		void pop_front()
		{
			erase(begin());
		}
		void pop_back(const T& x)
		{
			erase(--end());
		}
		iterator insert(iterator pos, const T& val)
		{
			Node* cur = pos._node;
			Node* newnode = new Node(val);
			Node* prev = cur->_prev;
			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
			return iterator(newnode);
		}
		iterator erase(iterator pos)
		{
			assert(pos != end());
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* next = cur->_next;
			prev->_next = next;
			next->_prev = prev;
			delete cur;
			return iterator(next);
		}
	private:
		Node* _head;
	};
}
